package com.zjj.lbw.microservice.sentinel.app.demo;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhanglei.zjj
 * @description Sentinel快速开始-使用@SentinelResource
 * @date 2023/7/28 11:42
 */
@RestController
@Slf4j
public class UserController {
    @SentinelResource(value = "User",
            blockHandler = "handleException",
            fallback = "fallbackException")
    @RequestMapping("/user")
    public String sayHello(Long id) throws InterruptedException {
        if (id == 1) {
            int i = 1 / 0;
        }

        // 模拟真实业务耗时
        Thread.sleep(900);
        return "hello zjj!";
    }

    public String handleException(Long id, BlockException ex) {
        return "被流控了";
    }

    public String fallbackException(Long id, Throwable t) {
        return "被异常降级了";
    }
}
